package com.welink.cmp.gatewayimpl.metadata;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.welink.cmp.convertor.GenericConvertor;
import com.welink.cmp.dao.metadata.SwitchDaoService;
import com.welink.cmp.dao.metadata.dataobject.SwitchDO;
import com.welink.cmp.domain.metadata.Switch;
import com.welink.cmp.domain.metadata.gateway.ISwitchGateway;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 交换机 Gateway 实现类
 */
@Component
@Primary
@Slf4j
public class SwitchGatewayImpl implements ISwitchGateway {

    @Autowired
    private SwitchDaoService switchDaoService;

    @Override
    public boolean saveOrUpdateSwitch(Switch sSwitch) {
        SwitchDO switchDO = new SwitchDO();
        BeanUtils.copyProperties(sSwitch, switchDO);
        return switchDaoService.saveOrUpdate(switchDO);
    }

    /**
     * @param switchList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveOrUpdateBatchBySn(List<Switch> switchList) {
        //过滤全部 SN
        final List<String> allSnList = switchList.stream().map(Switch::getSn).collect(Collectors.toList());
        LambdaQueryWrapper<SwitchDO> queryWrapper = Wrappers.lambdaQuery(SwitchDO.class);
        queryWrapper.select(SwitchDO::getSn).in(SwitchDO::getSn,allSnList);
        final List<SwitchDO> existSwList = switchDaoService.list(queryWrapper);
        // 已存在的记录 SN-ID
        final Map<String, Long> existSwMap = existSwList.stream().collect(Collectors.toMap(SwitchDO::getSn, SwitchDO::getId));
        switchList.forEach(switchO -> {
            final Long swId = existSwMap.get(switchO.getSn());
            if ( swId !=null){
                switchO.setId(swId);
            }
        });
        List<SwitchDO> allSwitchDoList = GenericConvertor.toDOList(switchList, SwitchDO.class);
        // 批量更新
        final boolean rst = switchDaoService.saveOrUpdateBatch(allSwitchDoList);
        log.info("根据 Sn 批量更新或插入交换机[{}]，更新Sn记录{},全部Sn{} ",rst,existSwList,switchList);
        return rst;
    }

    @Override
    public Switch getSwitchById(Long id) {
        SwitchDO switchDO = switchDaoService.getById(id);
        Switch aSwitch = new Switch();
        BeanUtils.copyProperties(switchDO, aSwitch);
        return aSwitch;
    }

    /**
     * @param ipList
     * @return
     */
    @Override
    public List<Switch> getSwitchListByIp(List<String> ipList) {
        LambdaQueryWrapper<SwitchDO> queryWrapper = Wrappers.lambdaQuery(SwitchDO.class);
        queryWrapper.in(SwitchDO::getManagementIp,ipList);
        final List<SwitchDO> existSwList = switchDaoService.list(queryWrapper);
        return GenericConvertor.toCOList(existSwList, Switch.class);
    }


}
